package com.jic.market.impl.rest.discount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.PromotionLimitRuleEntity;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.ConditionDiscountModel;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.PromotionLimitRuleEntityMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 满量-折
 * @author yinhaijun
 * @date: 2020/5/7
 */
@Service("FullNumDiscountProcessor")
public class FullNumDiscountProcessor extends AbstractConditionDiscountProcessor {
    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;
    @Autowired
    private PromotionLimitRuleEntityMapper promotionLimitRuleEntityMapper;
    @Override
    public ConditionResult isCondition(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(((DiscountBaseRequest)request).getThirdUserId(), ((DiscountBaseRequest)request).getAdminOperate().getChildId().toString(), conditionDiscountModel.getChannelCodes(), conditionDiscountModel.getPromotionMemberGradeList())){
            return new ConditionResult(false, "满量折计算"+conditionDiscountModel.getPromotionCode()+"会员等级校验未通过");
        }
        AtomicReference<Long> countValidNum = new AtomicReference<>(0L);
        List<String> thirdSkuIdList = conditionDiscountModel.getThirdSkuIdList();
        thirdSkuIdList.forEach((thirdSkuId)->{
            GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(thirdSkuId);
            PromotionLimitRuleEntity promotionLimitRuleEntity = promotionLimitRuleEntityMapper.getEntityByPromotionIdAndMpId(Long.parseLong(conditionDiscountModel.getPromotionId()), goodsDetailModel.getSkuId());
            LimitRuleResult limitRuleResult = checkCommonLimitRule(promotionLimitRuleEntity, conditionDiscountModel.getPromotionId(), goodsDetailModel, conditionDiscountModel.getFrontPromotionTypeEnum(), ((DiscountBaseRequest) request).getThirdUserId());
            countValidNum.set(countValidNum.get() + limitRuleResult.getValidNum());
        });

        // 超出购买按原价处理
        List<PromotionRuleEntity> promotionRuleEntityList = promotionRuleEntityMapper.queryByPromotionId(Long.parseLong(conditionDiscountModel.getPromotionId()));
        // 首先获取阶梯等级最高的数据
        BigDecimal contentValue = getFlightContentValue(conditionDiscountModel.getOverlayAppFlag(), conditionDiscountModel.getOverlayAppNum(),  new BigDecimal(countValidNum.get()), promotionRuleEntityList, 1);

        // 不优惠处理
        if(null == contentValue || contentValue.compareTo(BigDecimal.ZERO) == 0){
            return new ConditionResult(false, "满量折无优惠");
        }
        ConditionResult conditionResult = new ConditionResult(true, "有优惠");
        conditionResult.setObject(contentValue);
        return conditionResult;
    }

    @Override
    public void queryShoppingCartAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        querySettlementAfter(conditionResult, conditionDiscountModel, goodsDetailModelMap, request);
    }

    @Override
    public void querySettlementAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        //折扣
        BigDecimal zhekou = (BigDecimal) conditionResult.getObject();
        BigDecimal actualTotalAmount = conditionDiscountModel.getActualTotalAmount(goodsDetailModelMap);
        BigDecimal promotionDiscountTotalAmount = Money.multiply(actualTotalAmount, Money.subtract(new BigDecimal(1), Money.divide(zhekou, new BigDecimal(100))));
        conditionDiscountModel.setPromotionDiscountTotalAmount(promotionDiscountTotalAmount);
    }

}
